<?php
/*
 * File name:products.php
 * Create by DucNN
 * Create date: 29/10/2012
 */
class Products extends MX_Controller
{   
    function __construct() {
        parent::__construct();    
        $this->load->model('Restaurentmenus_model');                                       
    }
    
    function index($restaurent_id = '~',$keyword = '~', $row = 0)
    {
        $product = new Product();
        
        $data['key_word'] = urldecode($keyword);
        if($this->input->post('txtKeyWord'))
        {
            $data['key_word'] = $this->input->post('txtKeyWord');
        }
        
        //paging
        include('paging.php');        		                                 
        $data['restaurent_id'] = $restaurent_id;
        // compute total record     
        if($this->input->post('restaurent_id'))
        {
            $data['restaurent_id'] = $this->input->post('restaurent_id');
        }
        //echo $data['restaurent_id'];
        if($data['key_word'] != '~')
        {
            $product->like('name', $data['key_word']);
        }
        if ($data['restaurent_id'] != '~')
        {
            $product->where('restaurent_id',$data['restaurent_id']);
        } 
        $config['base_url']= base_url()."/administrator/products/index/".$data['restaurent_id'].'/'.$data['key_word'].'/';
        $config['total_rows'] = $product->count();        
        $config['cur_page'] = $row;		
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();	
        
        $product = new Product();
        $product->limit($config['per_page'], $row)
            ->order_by('id','DESC');                        
        if($data['key_word'] != '~')
        {
            $product->like('name', $data['key_word']);
        }
        if ($data['restaurent_id'] != '~')
        {
            $product->where('restaurent_id',$data['restaurent_id']);
        }               
        $data['list_product'] = $product->get();        
        // get list restaurent
        $res = new Restaurent();
        $data['list_res'] = $res->get();
        $data['view'] = 'products_index';
        $this->load->view('back_end/template_noright',$data);
    }
    function add()
    {
        $language = new Language();
        $language->where('status','enable')->get();
        $flag = FALSE;
        foreach ($language as $lang)
        {
            if($this->input->post('name_'.$lang->code))
            {
                $slug =$this->common->get_link($this->input->post('name_'.$lang->code),'Restaurent');
                $flag = TRUE;
            }
        }
        if ($flag)
        {
            $product = new Product();
            
            $productmeta_title = new Productmeta();
            $productmeta_title->meta_key = 'seo_title';
            
            $productmeta_des = new Productmeta();
            $productmeta_des->meta_key = 'seo_description';
            
            $productmeta_key = new Productmeta();
            $productmeta_key->meta_key = 'seo_keyword';
             
            foreach ($language as $lang)
            {
                if($this->input->post('name_'.$lang->code))
                {            
                    $name = $this->input->post('name_'.$lang->code);
                    //echo $name;
                    $price = $this->input->post('price');
                    $restaurent_id= $this->input->post('restaurent_id');      
                    //$fax = $this->input->post('fax_'.$lang->code);		
                    //$email = $this->input->post('email_'.$lang->code);    
                    //$address = $this->input->post('address_'.$lang->code);      
                    $description = $this->input->post('description_'.$lang->code);		
                    $content = $this->input->post('content_'.$lang->code);      
                    $status = $this->input->post('status');		
                    $image = $this->input->post('image');     
                    $create_date = $this->input->post('create_date');
                               
                    $seo_title = $this->input->post('seo_title_'.$lang->code);  
                    $seo_description = $this->input->post('seo_description_'.$lang->code);
                    $seo_keyword = $this->input->post('seo_keyword_'.$lang->code);
                          
                    
                             
                                                                        
                    $prefix = '<!--:'.$lang->code.'-->';
                    $suffix = '<!--:-->';
                    $product->name .= $prefix.$name.$suffix;                    
                    //$restaurent->fax .= $prefix.$fax.$suffix;
                    //$restaurent->email .= $prefix.$email.$suffix;
                    //$restaurent->address .= $prefix.$address.$suffix;
                    $product->description .= $prefix.$description.$suffix;
                    $product->content .= $prefix.$content.$suffix;
                    
                    //$restaurent->slug = $slug;
                                                                         
                    //add seo title                
                    $productmeta_title->meta_value .= $prefix.$seo_title.$suffix;                        
                    //add seo desciption                
                    $productmeta_des->meta_value .= $prefix.$seo_description.$suffix;                        
                    //add seo keywords                
                    $productmeta_key->meta_value .= $prefix.$seo_keyword.$suffix;                                                                                                           
                        
                }                          
            }
            if($create_date)
            {
                $format = 'd-m-Y H:i:s';
                $create_date = DateTime::createFromFormat($format, $create_date);
                $create_date = $create_date->format('Y-m-d H:i:s');                
            }
            else {
                $create_date = date('Y-m-d H:i:s');
            } 
            $product->price = $price;
            $product->status = $status;
            $product->image = $image;
            $product->create_date = $create_date;
            $product->slug = $slug;
            $product->restaurent_id = $restaurent_id;
            if($product->save())
            {
                $productmeta_title->save($product);
                $productmeta_des->save($product);
                $productmeta_key->save($product);
            } 
           redirect('administrator/products');
        } else
        {
            $language = new Language();
            $language->where('status','enable')->get();
            $data['list_lang'] = $language;
             // get list restaurent
            $res = new Restaurent();
            $data['list_res'] = $res->get(); 
            $data['view'] = 'products_add';
            $this->load->view('back_end/template_noright',$data);
        }
    }
    function edit($id = 0)
    {
         if($this->input->post('name'))
        {            
            $name = $this->input->post('name');
            //echo $name;
            $price = $this->input->post('price');
            $restaurent_id= $this->input->post('restaurent_id');        
            //$fax = $this->input->post('fax');		
            //$email = $this->input->post('email');    
            //$address = $this->input->post('address');      
            $description = $this->input->post('description');		
            $content = $this->input->post('content');      
            $status = $this->input->post('status');		
            $image = $this->input->post('image');     
            $create_date = $this->input->post('create_date');
                       
            $seo_title = $this->input->post('seo_title');  
            $seo_description = $this->input->post('seo_description');
            $seo_keyword = $this->input->post('seo_keyword');
                  
            
            if($create_date)
            {
                $format = 'd-m-Y H:i:s';
                $create_date = DateTime::createFromFormat($format, $create_date);
                $create_date = $create_date->format('Y-m-d H:i:s');                
            }
            else {
                $create_date = date('Y-m-d H:i:s');
            }            
            
            $slug = $this->input->post('slug');
            $slug =$this->common->get_link($slug,'Product');
        
        
            $product = new Product();
            $product->get_by_id($id);
            $product->name = $name;
            $product->price = $price;
            //$product->fax = $fax;
            //$product->email = $email;
            //$product->address = $address;
            $product->description = $description;
            $product->content = $content;
            $product->status = $status;
            $product->image = $image;
            $product->create_date = $create_date;
            $product->slug = $slug;
            $product->restaurent_id = $restaurent_id; 
            if($product->save())
            {
                
                //add seo title
                $productmeta = new Productmeta();
                $productmeta->where(array('product_id'=>$id,'meta_key'=>'seo_title'))->update('meta_value',$seo_title);
                
        
                //add seo desciption
                $productmeta = new Productmeta();
                $productmeta->where(array('product_id'=>$id,'meta_key'=>'seo_description'))->update('meta_value',$seo_description);
                        
                //add seo keywords
                $productmeta = new Productmeta();
                $productmeta->where(array('product_id'=>$id,'meta_key'=>'seo_keyword'))->update('meta_value',$seo_keyword);                                                     
            }
           redirect('administrator/products');
                
        } else
        {
            $product = new Product();
            $data['product'] = $product->get_by_id($id);
            $data['seo_title'] = $product->get_productmeta($id, 'seo_title');
            $data['seo_description'] = $product->get_productmeta($id,'seo_description');
            $data['seo_keyword'] = $product->get_productmeta($id,'seo_keyword');
            // get list restaurent
            $res = new Restaurent();
            $data['list_res'] = $res->get();
            $data['view'] = 'products_edit';
            $data['id'] = $id;
            $this->load->view('back_end/template_noright',$data);
        }
    }
    function delete()
    {
        $id = $this->input->post('param');
        
        $product = new Product();
        $product->get_by_id($id);
        
        $productmeta = new Productmeta();

        $productmeta->where('id',$id)->get();        
        $productmeta->where('product_id',$id)->get();

        $productmeta->delete_all($product);
        
        $product->delete();
        
    }        	
}
?>
